package com.knight.hadoop.day12.zuoye;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

public class LogBean implements Writable, WritableComparable<LogBean> {
	private String ip;
	private String remoteUser;
	private String status;
	private String bodyBytesSend;
	private String date;
	private String sessionId;
	private String requestURL;
	private String referal;
	private String userAgent;
	private boolean valid = true;// 判断数据是否合法

	private long stayTime;// 停留时常
	private int stepNum;

	public LogBean() {
	}

	public void set(String date, String ip, String sessionId, String requestURL, String referal, long stayTime,
			int stepNum) {
		this.date = date;
		this.ip = ip;
		this.sessionId = sessionId;
		this.requestURL = requestURL;
		this.referal = referal;
		this.stayTime = stayTime;
		this.stepNum = stepNum;
	}

	public void set(String ip,String remoteUser,String status,String bodyBytesSend,String date, String requestURL, String referal,String userAgent) {
		this.ip = ip;
		this.remoteUser = remoteUser;
		this.status = status;
		this.bodyBytesSend = bodyBytesSend;
		this.date = date;
		this.requestURL = requestURL;
		this.referal = referal;
		this.userAgent=userAgent;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getRequestURL() {
		return requestURL;
	}

	public void setRequestURL(String requestURL) {
		this.requestURL = requestURL;
	}

	public String getReferal() {
		return referal;
	}

	public void setReferal(String referal) {
		this.referal = referal;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(valid);        
		sb.append("\001").append(ip);
		sb.append("\001").append(date);   
		sb.append("\001").append(remoteUser);
		sb.append("\001").append(status);
		sb.append("\001").append(bodyBytesSend);
		sb.append("\001").append(requestURL);   
		sb.append("\001").append(referal);
		sb.append("\001").append(userAgent);    
		return sb.toString();
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(ip);
		out.writeUTF(remoteUser);
		out.writeUTF(status);
		out.writeUTF(bodyBytesSend);
		out.writeUTF(date);
		//out.writeUTF(sessionId);
		out.writeUTF(requestURL);
		out.writeUTF(referal);
		out.writeUTF(userAgent);
		out.writeBoolean(valid);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		ip = in.readUTF();
		remoteUser = in.readUTF();
		status = in.readUTF();
		bodyBytesSend = in.readUTF();
		date = in.readUTF();
//		sessionId = in.readUTF();
		requestURL = in.readUTF();
		referal = in.readUTF();
		userAgent = in.readUTF();
		valid = in.readBoolean();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LogBean other = (LogBean) obj;
		if (this.getDate().equals(other.getDate()) && this.getIp().equals(other.getIp())
				&& this.getReferal().equals(other.getReferal()) && this.getRequestURL().equals(other.getRequestURL())
				&& this.getSessionId().equals(other.getSessionId())) {
			return true;
		}
		return false;
	}

	public int getStepNum() {
		return stepNum;
	}

	public void setStepNum(int stepNum) {
		this.stepNum = stepNum;
	}

	public long getStayTime() {
		return stayTime;
	}

	public void setStayTime(long stayTime) {
		this.stayTime = stayTime;
	}

	@Override
	public int compareTo(LogBean o) {
		Text ipText = new Text();
		ipText.set(ip);
		Text ipText2 = new Text();
		ipText2.set(o.getIp());
		int cmp = ipText.compareTo(ipText2);
		if (cmp == 0) {
			cmp = Utils.compareDateSec(date, o.getDate()) > 0 ? 1 : -1;
		}
		return cmp;
	}

	public String getRemoteUser() {
		return remoteUser;
	}

	public void setRemoteUser(String remoteUser) {
		this.remoteUser = remoteUser;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getBodyBytesSend() {
		return bodyBytesSend;
	}

	public void setBodyBytesSend(String bodyBytesSend) {
		this.bodyBytesSend = bodyBytesSend;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}
}
